package com.hnjd.dao;

import java.util.List;

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.One;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.springframework.web.bind.annotation.RequestMapping;

import com.hnjd.entity.Carts;
import com.hnjd.entity.Goods;
import com.hnjd.entity.Items;
import com.hnjd.entity.Orders;
import com.hnjd.entity.Tops;
import com.hnjd.entity.Users;
import com.hnjd.util.PageUtil;

public interface IndexDao {

	//首页查询
	@Select("select * from goods inner join tops on goods.id=tops.good_id")
	List<Goods> indexList();
	
	//登录
	@Select("select * from users where username=#{param1} and password=#{param2}")
	Users login(String username,String password);
	
	//注册
	@Insert("insert into users values(null,#{username},#{password},#{name},#{phone},#{address})")
	Integer register(Users users);
	
	//查找Orders的id
	@Select("select id from orders where name=#{name}")
	int[] ordersById(String name);
	
	//添加地址
	@Update("update orders set address=#{address},name=#{name},phone=#{phone} where id=#{id}")
	Integer addressUpdate(Orders orders);
	
	//修改密码
	@Update("update users set password=#{param1} where id=#{param2}")
	Integer passwordUpdate(String passwordNew,Integer id);
	
	//订单查询
	@Select("select * from orders")
	List<Orders> orderList();
	
	//根据orders的id查询items
	@Select("select * from items where order_id=#{order_id}")
	List<Items> itemsByOrderId(Integer order_id);
	
	//查询items对应的goods
	@Select("select * from goods where id=#{id}")
	Goods itemsByGoodsId(Integer id);
	
	//购物车根据userid查询carts 分页
	@Select("select * from carts where user_id=#{param1} limit #{param2.start},#{param2.pageSize}")
	List<Carts> cartsByUserId(Integer user_id,PageUtil page);
	
	//购物车根据userid查询carts
	@Select("select * from carts where user_id=#{param1}")
	List<Carts> cartsByUserList(Integer user_id);
	
	//购物车根据good_id查询goods
	@Select("select * from goods where id=#{id}")
	Goods goodsById(Integer id);
	
	
	//查询购物车的总数量
	@Select("select count(*) from carts where user_id=#{user_id}")
	Integer cartsNum(Integer user_id);
	
	//购物车根据user_id查询total
	@Select("select total from orders where user_id=#{user_id}")
	Integer[] orderByUserId(Integer user_id); 
	
	//修改购物车商品数量
	@Update("update carts set amount=amount+#{param2} where id=#{param1}")
	Integer cartUpdate(Integer id,Integer amount);
	
	//删除购物车商品
	@Delete("delete from carts where id=#{id}")
	Integer cartDelete(Integer id);
	
	//添加购物车
    @Insert("insert into carts value(null,1,#{param1},#{param2})")
    Integer addCart(Integer goodid,Integer userid);
    
    //判断购物车中是否含有这个商品
    @Select("select count(*) from carts where good_id=#{param1} and user_id = #{param2}")
    Integer cartExict(Integer goodid,Integer userid );
    
    //存在相同商品amount加一
    @Update("update carts set amount=amount+1 where good_id=#{param1} and user_id = #{param2}")
    Integer addAmount(Integer goodid,Integer userid );
    
    @Select("SELECT tops.*,goods.* FROM tops INNER JOIN goods ON tops.good_id=goods.id ")
	public List<Goods> findGoodsById();
	
	@Select("SELECT tops.*,goods.* FROM tops INNER JOIN goods ON tops.good_id=goods.id limit #{start},#{pageSize} ")
	List<Goods> findGoods(PageUtil page);
	
	//查询所有热销商品
	@Select("SELECT * from goods order by sales DESC")
	List<Goods>  findSalesGoods();
	
	@Select("select * from tops")
	List<Tops> findAllTops();

	 
	@Insert("insert into tops values(null,#{type},#{goodId})")
	void topSave(Tops top);

	@Delete("delete from tops where good_id=#{goodId}")
	void topDelete(Integer goodId);
	
	//通过用户id查找对应的购物车信息
		@Select("select * from carts where user_id=#{userId}")
		//mybatis只能做简单数据类型的映射，复杂对象不能实现自动映射，需要做手动映射
		@Results(id = "CartsAndGoods",value= {
			//将数据库的字段映射到实体类的属性上 property是实体类的属性值  column是数据库中的字段名
			@Result(property = "id",column = "id"),
			@Result(property = "user_id",column = "user_id"),
			@Result(property = "good_id",column = "good_id"),
			@Result(property = "amount",column = "amount"),
			//一对一映射  要调用com.ws.dao.GoodsDao类的findById方法
			@Result(property = "good",column = "good_id",one=@One(select="com.hnjd.dao.GoodsMapper.findGoodById"))
		})
		List<Carts> findCartsByUserId(Integer userId);
		
		//根据用户id删除所有信息
		@Delete("delete from carts where user_id=#{userId}")
		Integer DeleteByUserId(@Param("userId") Integer userId);
		
		//生成订单
		@Insert("insert orders values (null,#{total},#{amount},1,1,#{name},#{phone},#{address},now(),#{userId})")
		@Options(useGeneratedKeys = true,keyProperty = "id",keyColumn = "id") //将自动生成的主键存入order中的id字段
		Integer generetorOrder(Orders order);
		
		//新增
		@Insert("insert items values (null,#{price},#{amount},#{order_id},#{good_id})")
		public Integer add(Items item);
		
		
}
